package com.fouram.web.handler;

import com.fouram.core.domain.R;
import com.fouram.core.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MultipartException;

/**
 * 报错异常捕获 = 每个子框架 XXXExceptionHandler + GlobalExceptionHandler
 * @author Liup
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

	/**
	 * 异常处理
	 */
	@ExceptionHandler(Throwable.class)
	public R handlerGlobalException(Throwable ex) {
		log.error("{}", ex);
		if (ex instanceof MultipartException) {
			return R.fail("上传文件超过最大限制.");
		} else if (ex instanceof BizException) {
			return R.fail(ex.getMessage());
		} else {
			return R.fail("系统繁忙，请稍候再试.");
		}
	}
}
